//引入express
const express = require('express')
//使用express
const app = express()
//跨域引入的包npm install cors
app.use(require('cors')())
//express.json函数，将数据转换为json格式
app.use(express.json())

//引入函数,nodejs与mysql进行交互
const { execSQL } = require('../db/mysql')

// app.get('/', async (req, res) => {
//     const sql = `select * from registered;`
//     execSQL(sql, (err, result) => {
//         if (err) {
//             console.log('error', err);
//             return

//         }
//         console.log('result', result);

//     })
// })


//获取注册表的数据,实际做的select * from registered的内容返回到界面http://localhost:3001/api
app.get('/api', async (req, res) => {
    const sql = `select * from registered;`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })

})

//获取注册页非管理员信息
app.get('/api/admin', async (req, res) => {
    const sql = `select * from registered where status!='admin';`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })

})

//接收前端的参数，并与MySQL进行交互,
app.post('/api', async (req, res) => {
    // const sql = `select * from registered;`
    // execSQL(sql).then(result => {
    //     console.log(result);
    //     res.send(result)
    // })

    // node.js到的参数req.body
    console.log(req.body.name);
    const sql = `select * from registered where username="${req.body.name}";`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })


})

app.post('/api/students', async (req, res) => {
    //req.body{}
    const info = req.body
    console.log(info);
    console.log(typeof info.name);
    console.log(info.pass);
    console.log(info.checkPass);
    console.log(info.email);
    const sql = `insert into registered(username,password,sure,email,address) values("${info.name}",${info.pass},${info.checkPass},"${info.email}","${info.address}");`
    // const sql = `insert into registered values("a",123,123,"b");`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })

})

//增加浏览量
// 1.post接收前端发过来的参数
// 2.前端发过来的参数都在req.body，结构不友好
// 3.Object.keys(req.query), addViewNumberName[0]解析出前端发过来的参数
// 4.写MySQL语句放入到变量sql中
// 5.变量放入封装好的函数中与MySQL进行交互

app.post('/api/addViewNumber', async (req, res) => {
    const info = req.body
    console.log(req);
    console.log(req.query);
    // Object.keys{[{}]}
    let addViewNumberName = Object.keys(req.query)
    console.log(Object.keys(req.query));
    console.log(addViewNumberName[0]);

    const sql = `Update sources set view=view+1 where name='${addViewNumberName[0]}';`
    // const sql = `insert into registered values("a",123,123,"b");`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })

})

//主页(推荐页)
//1.get
//2.写MySQL语句放入到变量sql中
//3.变量放入封装好的函数中与MySQL进行交互
app.get('/api/sourcesLimit', async (req, res) => {
    const sql = `select * from sources order by view desc limit 9;`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })
});

//资源页
app.get('/api/sourcesList', async (req, res) => {
    const sql = `select * from sources;`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })
});


//获取讨论区的数据
app.get('/api/discuss', async (req, res) => {
    const sql = `select * from discuss;`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })

})

//发表文章
// 1.post接收前端发过来的参数
// 2.前端发过来的参数都在req.body，结构不友好
// 3.Object.keys(req.query), addViewNumberName[0]解析出前端发过来的参数
// 4.写MySQL语句放入到变量sql中
// 5.变量放入封装好的函数中与MySQL进行交互
app.post('/api/publish', async (req, res) => {
    const info = req.body
    // {name:'wj',n:"",desc:"x"}
    console.log(info);
    console.log(info.name);
    console.log(info.n);
    console.log(info.desc);
    const sql = `insert into discuss(discussionName,name,content) values("${info.name}","${info.n}","${info.desc}");`
    // const sql = `insert into registered values("a",123,123,"b");`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })

})

//删除用户：
// req.params.option:参数
// ``(tab上面的键):模板字符串,${变量}
// `delete from registered where username="${req.params.option}";`
// 'delete from registered where username="' + req.params.option+'";'
app.delete('/api/admin/:option', async (req, res) => {
    console.log(req.params.option);
    const sql = `delete from registered where username="${req.params.option}";`
    execSQL(sql).then(result => {
        console.log(result);
        //返回
        res.send({
            status: true
        })
    })

})

//删除评论：
// req.params.option:参数
// ``(tab上面的键):模板字符串,${变量}
// `delete from discuss where discussionName="${req.params.option}";`
// 'delete from discuss where discussionName="' + req.params.option+'";'
app.delete('/api/admin1/:option', async (req, res) => {
    console.log(req.params.option);
    const sql = `delete from discuss where discussionName="${req.params.option}";`
    execSQL(sql).then(result => {
        console.log(result);
        //返回
        res.send({
            status: true
        })
    })

})

//上传资源
app.post('/api/upload', async (req, res) => {
    const info = req.body
    console.log(info);
    console.log(info.name);
    console.log(info.link);
    console.log(info.desc);
    console.log(info.uploader);

    const sql = `insert into sources(name,link,details) values("${info.name}","${info.link}","${info.desc}");`
    // // const sql = `insert into registered values("a",123,123,"b");`
    execSQL(sql).then(result => {
        console.log(result);
        res.send(result)
    })

})

//关闭连接
// connection.end()

//监听端口
app.listen(3001, () => {
    console.log('http://localhost:3001/')
})